<template>
    <div class="tools-div">
        <el-button type="success" size="small" @click="addShow">添 加</el-button>
    </div>

    <el-table :data="list" style="width: 100%; margin-bottom: 20px" row-key="id" border default-expand-all>
        <el-table-column prop="title" label="菜单标题" />
        <el-table-column prop="component" label="路由名称" />
        <el-table-column prop="sortValue" label="排序" />
        <el-table-column prop="status" label="状态" #default="scope">
            {{ scope.row.status == 1 ? '正常' : '停用' }}
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="160" />
        <el-table-column label="操作" align="center" width="280" #default="scope">
            <el-button type="success" size="small" @click="addShow(scope.row)">
                添加下级节点
            </el-button>
            <el-button type="primary" size="small" @click="editShow(scope.row)">
                修改
            </el-button>
            <el-button type="danger" size="small" @click="remove(scope.row.id)">
                删除
            </el-button>
        </el-table-column>
    </el-table>



    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
        <el-form label-width="120px">
            <el-form-item label="菜单标题">
                <el-input v-model="sysMenu.title"/>
            </el-form-item>
            <el-form-item label="路由名称">
                <el-input v-model="sysMenu.component"/>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="sysMenu.sortValue"/>
            </el-form-item>
            <el-form-item label="状态">
                <el-radio-group v-model="sysMenu.status">
                    <el-radio :label="1">正常</el-radio>
                    <el-radio :label="0">停用</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="saveOrUpdate">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script setup>
import { ref, onMounted } from "vue"
import { FindNodes,SaveData,UpdateById,RemoveSysMenuById } from "@/api/sysMenu"

import { ElMessage,ElMessageBox } from "element-plus"

// 定义表格数据模型
const list = ref([])
// const list = ref([
    // {
    //     "id": 1,
    //     "title": "系统管理",
    //     "component": "system",
    //     "sortValue": 1,
    //     "status": 1,
    //     "createTime": "2023-05-04",
    //     "children":[
    //         {"id":2, "title": "用户管理" , "component":"sysUser" , "sortValue":1 ,"status":1 , "createTime":"2023-05-04"},
    //         {"id":3, "title": "角色管理" , "component":"sysRole" , "sortValue":2 ,"status":1 , "createTime":"2023-05-04"},
    //         {"id":4, "title": "菜单管理" , "component":"sysMenu" , "sortValue":3 ,"status":1 , "createTime":"2023-05-04"}
    //     ]

    // },
    // {
    //     "id": 5,
    //     "title": "基础数据管理",
    //     "component": "base",
    //     "sortValue": 2,
    //     "status": 1,
    //     "createTime": "2023-05-04",
    //     "children":[
    //         {"id":6, "title": "商品单位" , "component":"productUnit" , "sortValue":2 ,"status":1 , "createTime":"2023-05-04"},
    //         {"id":7, "title": "地区管理" , "component":"region" , "sortValue":1 ,"status":1 , "createTime":"2023-05-04"}
    //     ]
    // }
// ])

const dialogVisible = ref(false)

const dialogTitle = ref('添加')

const defaultForm = {
    id: '',
    parentId: 0,
    title: '',
    url: '',
    component: '',
    icon: '',
    sortValue: 1,
    status: 1,
}

const sysMenu = ref(defaultForm)

//======================================================

onMounted(() => {
    fetchData()
})

const fetchData = async () => {
    const { code, message, data } = await FindNodes()
    list.value = data
}

const addShow = (row) => {
    sysMenu.value = {}
    dialogVisible.value = true    
    
    if(!row.id){
        dialogTitle.value = '添加'
        sysMenu.value.parentId = 0
    }else{
        dialogTitle.value = '添加下级节点'
        sysMenu.value.parentId = row.id
    }
}

const saveOrUpdate = () => {
    //debugger
    if(!sysMenu.value.id){        
        save()
    }else{
        update()
    }
}

const save = async () => {
    SaveData(sysMenu.value);
    dialogVisible.value = false
    ElMessage.success('操作成功')
    fetchData()
}

const update = async () => {
    UpdateById(sysMenu.value);
    dialogVisible.value = false
    ElMessage.success('操作成功')
    fetchData()
}

const editShow = (row) => {
    sysMenu.value = row 
    dialogVisible.value = true
}


const remove = async (id) => {
    console.log('removeDataById:' + id)
    ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(
        async () => {
        //debugger
        const { code , message ,data } = await RemoveSysMenuById(id)
        
        if(code === 200) {        
            fetchData()
            ElMessage.success('删除成功')
        }else {
            ElMessage.error(message)
        }      
    }).catch(() => {
        ElMessage.info('取消删除')
    })
}
</script>

<style scoped>
.search-div {
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}

.tools-div {
    margin: 10px 0;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}
</style>